// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/common/config/config.h>
#include <pollux/common/file/temp_directory_path.h>

#include <azure/storage/blobs/blob_container_client.hpp>
#include <azure/storage/blobs/block_blob_client.hpp>
#include <fmt/format.h>
#include <pwd.h>
#include <unistd.h>
#include <iostream>
#include "boost/process.hpp"

namespace kumo::pollux::filesystems {

using namespace Azure::Storage::Blobs;
static std::string_view kAzuriteServerExecutableName{"azurite-blob"};
static std::string_view kAzuriteSearchPath{":/usr/bin/azurite"};

class AzuriteServer {
 public:
  AzuriteServer(int64_t port);

  const std::string connectionStr() const;

  void start();

  std::string URI() const;

  std::string fileURI() const;

  std::string container() const {
    return container_;
  }

  std::string file() const {
    return file_;
  }

  std::shared_ptr<const config::ConfigBase> hiveConfig(
      const melon::F14FastMap<std::string, std::string> configOverride = {})
      const;

  void stop();

  bool isRunning();

  void addFile(std::string source);

  virtual ~AzuriteServer();

 private:
  int64_t port_;
  const std::string account_{"test"};
  const std::string container_{"test"};
  const std::string file_{"test_file.txt"};
  const std::string key_{
      "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="};
  std::vector<std::string> commandOptions_;
  std::unique_ptr<::boost::process::child> serverProcess_;
  boost::filesystem::path exePath_;
  boost::process::environment env_;
};
} // namespace kumo::pollux::filesystems
